using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Skill_Shard : Skill_Base
{
    private SkillObject_Shard currentShard;
    private Player_Health playerHealth;

    [Header("-----Shard Details-----")]
    [SerializeField] private GameObject shardPrefab;
    [SerializeField] private float lifeDuration = 2f;

    [Header("-----Moving Shard Upgrade-----")]
    [SerializeField] private float moveSpeed = 3f;

    [Header("-----Multicast Shard Upgrade-----")]
    [SerializeField] private float currentCharge;
    [SerializeField] private float maxCharge = 3;
    [SerializeField] private bool isRecharge;

    [Header("-----Teleport Shard Upgrade-----")]
    [SerializeField] private float teleportDuration;

    [Header("-----TeleportHpRewind Shaed Upgerade-----")]
    private float savedPlayerHealthPercent;

    protected override void Awake()
    {
        base.Awake();
        playerHealth = GetComponentInParent<Player_Health>();
        currentCharge = maxCharge;

    }
    
    //创建碎片
    public void CreateShard()
    {
        GameObject shard = PoolManager.Instance.Release(shardPrefab, (shard) =>
        {
            shard.transform.position = transform.position;
            shard.transform.rotation = Quaternion.identity;
        });
            
        currentShard = shard.GetComponent<SkillObject_Shard>();
        currentShard.SetupShard(this, GetDetonateTime());
        if (Unlock(SkillUpgradeType.Shard_Teleport) || Unlock(SkillUpgradeType.Shard_TeleportHpRewind))
            currentShard.onExplode += ForceCooldown;
    }
    public void CreateRawShard(float duration,Transform domainTarget = null,bool isDomain = false)
    {
        bool canMove = isDomain != false ? isDomain : Unlock(SkillUpgradeType.Shard_MoveToEnemy) || Unlock(SkillUpgradeType.Shard_Multicast);
        GameObject shard = PoolManager.Instance.Release(shardPrefab, (shard) =>
        {
            shard.transform.position = transform.position;
            shard.transform.rotation = Quaternion.identity;
        });
        shard.GetComponent<SkillObject_Shard>().SetupShard(this, canMove, moveSpeed, duration, domainTarget);
    }

    public override bool CanUseSkill()
    {
        if (base.CanUseSkill() == false)
            return false;
        if (player.stateMachine.currentState == player.domainExpansionState)
            return false;
        return true;
    }
    public override void TryUseSkill()
    {
        
        if (CanUseSkill() == false)
            return;
        
        switch (skillUpgradeType)
            {
                case SkillUpgradeType.Shard: HandleShardRegular(); return;
                case SkillUpgradeType.Shard_MoveToEnemy: HanadleShardMoving(); return;
                case SkillUpgradeType.Shard_Multicast: HandleShardMulticast(); return;
                case SkillUpgradeType.Shard_Teleport: HandleShardTeleprot(); return;
                case SkillUpgradeType.Shard_TeleportHpRewind: HnadleShardTelerportRewind(); return;
                default: return;
            }
    }

    #region General Shard
    //普通技能：普通碎片
    private void HandleShardRegular()
    {
        CreateShard();
        SetSkillOnCoolDown();
    }
    #endregion

    #region Skill 1.1 Shard move to Enemy
    //技能升级1.1：碎片自动追踪敌人
    private void HanadleShardMoving()
    {
        HandleShardRegular();
        currentShard.MoveToClosestTarget(moveSpeed);
    }
    #endregion

    #region Skill 1.2 Can save three shard
    //技能升级1.2：碎片充能三个
    private void HandleShardMulticast()
    {
        if (currentCharge <= 0)
            return;
        CreateShard();
        currentShard.MoveToClosestTarget(moveSpeed);
        currentCharge--;
        if (isRecharge == false)
            StartCoroutine(ReChargeShardCo());
    }
    private IEnumerator ReChargeShardCo()
    {
        isRecharge = true;
        while (currentCharge < maxCharge)
        {
            yield return new WaitForSeconds(cooldown);
            currentCharge++;
        }
        isRecharge = false;
    }
    #endregion
   
    #region Skill 2.1 Swap position in Shard and Player
    private void HandleShardTeleprot()
    {
        if (currentShard == null || currentShard.gameObject.activeSelf == false)
            CreateShard();
        else
        {
            SwapShardAndPlayer();
            SetSkillOnCoolDown();
        }
    }
    #endregion
   
    #region Skill 2.2 Swap position in Shard and Player ,and rewind the health ahead in the shard to be release
    private void HnadleShardTelerportRewind()
    {
        if (currentShard == null || currentShard.gameObject.activeSelf == false)
        {
            CreateShard();
            savedPlayerHealthPercent = playerHealth.GetCurrentHealthPercent();
        }
        else
        {
            SwapShardAndPlayer();
            SetSkillOnCoolDown();
            playerHealth.SetCurrentHealthPercent(savedPlayerHealthPercent);
        }
    }
    #endregion

    #region Skill 3.1 Shard and Dash Skill,When Dash Skill to be release ,release a Shard
    
    #endregion
    
    //交换碎片和玩家的位置
    private void SwapShardAndPlayer()
    {
        Vector2 shardPostion = currentShard.transform.position;
        currentShard.transform.position = player.transform.position;
        player?.SetTeleportPosition(shardPostion);
        currentShard.Explode();
    }

    //得到当前技能的冷却时间
    private float GetDetonateTime()
    {
        if (Unlock(SkillUpgradeType.Shard_Teleport) || Unlock(SkillUpgradeType.Shard_TeleportHpRewind))
            return teleportDuration;
        return lifeDuration;
    }

    private void ForceCooldown()
    {
        if (IsCoolDown() == false)
        {
            SetSkillOnCoolDown();
            currentShard.onExplode -= ForceCooldown;
        }
    }
}
